<template>
  <div class="pad-20 bg-color-white">
      <div>
        <el-button @click="handleAdd" type="primary">新增</el-button>
      </div>
      <!--顶部搜索 -->
      <div class="top-operate-default mar-t-10 mar-b-10">
        <div class="search-item">
          <label>搜索：</label>
          <el-input @keyup.enter.native="handleSearch" v-model.trim="searchListQuery.invoiceContent" placeholder="请输入开票内容"></el-input>
          <el-button-group class="mar-l-20">
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button plain @click="resetSearch">重置</el-button>
          </el-button-group>
        </div>
      </div>

      <!-- 内容列表 -->
      <div class="table-container">
        <el-table ref="multipleTable" :data="dataList" v-loading="listLoading" element-loading-text="加载中">
          <el-table-column label="开票内容" min-width="200px" align="left">
            <template slot-scope="scope">
              {{scope.row.invoiceContent}}
            </template>
          </el-table-column>
          <el-table-column min-width="200px" label="操作" align="right" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" size="medium" @click="handleEdit(scope.row)">编辑</el-button>
              <el-popconfirm
                title="确定删除吗？"
                @onConfirm="handleDelete(scope.row)"
              >
                <el-button slot="reference" class="mar-l-10" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 列表分页 -->
      <div class="pagination">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="searchListQuery.currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="searchListQuery.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    

    <el-dialog :title="dialogTitle" v-el-drag-dialog :visible.sync="invoiceContentVisible" :close-on-press-escape="true" :center="true" width="600px">
      <el-form ref="invoiceContentForm" :model="invoiceContentForm" label-position="right" :rules="rules" label-width="120px">
          <el-form-item label="开票内容" prop="invoiceContent" >
          <el-input v-model="invoiceContentForm.invoiceContent" class="width-per-70" placeholder="请输入开票内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="invoiceContentVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSave">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import elDragDialog from '@/directive/el-dragDialog'
  import { pageInvoiceContent, saveInvoiceContent, deleteInvoiceContent } from '@/api/invoice/invoiceContent'

  export default {
    components: { },
    directives: {
      elDragDialog
    },
    data() {
      return {
        total: 0,
        // 表格加载标识
        listLoading: true,
        // 新增开票内容弹窗
        invoiceContentVisible: false,
        multipleSelection: [],
        // 查询表单
        searchListQuery: {
          currentPage: 1,
          size: 10,
          enterpriseId: this.getEnterpriseId(),
          invoiceContent: ''
        },
        // 弹窗标题
        dialogTitle: '',
        // 发票内容表单
        invoiceContentForm: {
          invoiceContentId: '',
          invoiceContent: ''
        },
        rules: {
          invoiceContent: [
            { required: true, message: '请输入发票内容', trigger: 'blur' }
          ]
        },
        allChecked: false,
        // 表格数据
        dataList: []
      }
    },
    // 初始化页面
    created() {
      this.initList()
    },
    mounted() {
    },
    methods: {
      // 初始化
      initList() {
        this.listLoading = true
        pageInvoiceContent(this.searchListQuery).then(response => {
          if (response.isError) return
          this.dataList = response.data.records
          this.total = response.data.total
          this.listLoading = false
        })
      },
      // 勾选事件
      handleSelectionChange(val) {
        this.multipleSelection = val
        if (this.multipleSelection.length === this.dataList.length) {
          this.allChecked = true
        } else {
          this.allChecked = false
        }
      },
      // 查询事件
      handleSearch() {
        this.searchListQuery.currentPage = 1
        this.initList()
      },
      // 重置事件
      resetSearch() {
        this.searchListQuery = {
          currentPage: 1,
          size: 10,
          enterpriseId: this.getEnterpriseId(),
          invoiceContent: ''
        }
        this.initList()
      },
      // 开票内容保存
      handleSave() {
        this.$refs['invoiceContentForm'].validate((valid) => {
          if (valid) {
            var form = {
              enterpriseId: this.getEnterpriseId(),
              invoiceContent: this.invoiceContentForm.invoiceContent,
              invoiceContentId: this.invoiceContentForm.invoiceContentId
            }
            saveInvoiceContent(form).then((response) => {
              if (response.isError) return
              this.$message({ type: 'success', message: '保存成功!' })
              this.invoiceContentVisible = false
              this.initList()
            })
          }
        })
      },
      // 新增事件
      handleAdd() {
        this.dialogTitle = '新增开票内容'
        this.invoiceContentForm = {
          invoiceContentId: ''
        }
        if (this.$refs['invoiceContentForm']) this.$refs['invoiceContentForm'].resetFields()
        this.invoiceContentVisible = true
      },
      // 编辑事件
      handleEdit(row) {
        this.dialogTitle = '编辑开票内容'
        Object.assign(this.invoiceContentForm, row)
        this.invoiceContentVisible = true
      },
      // 删除事件（校验）
      handleDelete(row) {
        const ids = []
        if (row.invoiceContentId) {
          ids.push(row.invoiceContentId)
        } else {
          this.multipleSelection.forEach(o => {
            ids.push(o.invoiceContentId)
          })
        }
        if (ids.length === 0) {
          this.$message({ message: '请选择要删除的数据', type: 'warning' })
          return
        }
        this.doDelete(ids)
      },
      // 删除事件
      doDelete(ids) {
        deleteInvoiceContent({ invoiceContentIds: ids }).then(response => {
          if (response.isError) return
          this.$message({ type: 'success', message: '删除成功!' })
          this.initList()
        })
      },
      // 更改当前页数
      handleCurrentChange(val) {
        this.searchListQuery.currentPage = val
        this.initList()
      },
      // 更改当前页面大小
      handleSizeChange(val) {
        this.searchListQuery.size = val
        this.initList()
      }
    }
  }
</script>
<style lang="less" scoped>
  .top-operate {
    margin-bottom: 0;
    height: 85px;
    background-color: #f7f8fa;
    display: flex;
    align-items: center;
    white-space: nowrap;
    flex-wrap: nowrap;
    width: 100%;
    overflow-x: auto;
  }

  .search-item {
    flex-shrink: 0;
    padding: 0 20px;
    > div,
    .el-cascader {
      margin-right: 0px;
    }
     label{
      font-weight: unset;
    }
    .el-input {
      width: 200px;
    }
  }

  .btn-search {
    height: 60px;
    display: flex;
    align-items: center;
  }
  .table-container{
    /deep/.el-table th > .cell{
      padding-left: 14px;
    }
  }
    .bgc-default {
    /deep/ .el-dialog__body {
      padding-bottom: 140px;
    }
  }
</style>
